package com.clz.servlet;

import com.clz.servise.Impl.UserServiceImpl;
import com.clz.servise.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

@WebServlet("/login")
public class LoginServlet extends HttpServlet {
    private UserService userService=new UserServiceImpl();
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name =req.getParameter("username");
        String psw=req.getParameter("password");
        String noLogin=req.getParameter("noLogin");
        if (userService.login(name, psw)){
            if (noLogin!=null){
                //创建cookie
                Cookie came=new Cookie("username",name);
                //设置cookie有效期
                came.setMaxAge(1000);
                resp.addCookie(came);
                resp.sendRedirect("main.jsp");
            }
            resp.sendRedirect("main.jsp");
        }else {
//            resp.sendRedirect("login.html");
            resp.sendRedirect("login.html?login=false1");
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }
}
